package com.springboot.orderno.util;

import lombok.SneakyThrows;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTimeUtil {
	/**
	 * 获取n日之前或者之后的日期
	 * 
	 * @return
	 */
	public static String format(String dateTime, String srcFormat, String destFormat) {
		SimpleDateFormat srcSimpleDateFormat = new SimpleDateFormat(srcFormat);
		Date date = null;
		try {
			date = srcSimpleDateFormat.parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		if (null == date) {
			return null;
		}

		SimpleDateFormat destSimpleDateFormat = new SimpleDateFormat(destFormat);
		return destSimpleDateFormat.format(date);
	}

	/**
	 * 
	 * @param minutes
	 * @return
	 */
	public static String afterNowByMinutes(int minutes) {
		long dstTime = new Date().getTime() + minutes * 60 * 1000;
		Date dstDate = new Date(dstTime);
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dstDate);
	}

	/**
	 * 获取当前的年月日时分秒
	 * 
	 * @return
	 */
	public static String getCurrentDataTime() {
		Date date = new Date();
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(date);
	}

	/**
	 * 获取当前的年月日
	 * 
	 * @return
	 */
	public static String getCurrentData() {
		Date date = new Date();
		// 转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

	private static final int FIRST_DAY = Calendar.MONDAY;

	private static void printWeekdays() {
		Calendar calendar = Calendar.getInstance();
		setToFirstDay(calendar);
		for (int i = 0; i < 7; i++) {
			printDay(calendar);
			calendar.add(Calendar.DATE, 1);
		}
	}

	private static void setToFirstDay(Calendar calendar) {
		while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
			calendar.add(Calendar.DATE, -1);
		}
	}

	private static void printDay(Calendar calendar) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println(dateFormat.format(calendar.getTime()));
	}

	/**
	 * 获取当前月第一天
	 * 
	 * @return
	 */
	public static String getCurrentMonthFirstDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		return format.format(calendar.getTime());
	}

	/**
	 * 获取当前月最后一天
	 * 
	 * @return
	 */
	public static String getCurrentMonthLastDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return format.format(calendar.getTime());
	}

	/**
	 * 获取n日之前或者之后的日期
	 * 
	 * @return
	 */
	public static String getDay(int n) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, n);
		return simpleDateFormat.format(calendar.getTime());
	}

	/**
	 * 获取n月之前或者之后的月份
	 * 
	 * @return
	 */
	public static String getMonth(int n) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, n);
		return simpleDateFormat.format(calendar.getTime());
	}

	/**
	 * 获取n年之前或者之后的年份
	 * 
	 * @return
	 */
	public static String getYear(int n) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, n);
		return simpleDateFormat.format(calendar.getTime());
	}

	/**
	 * 获取当前月第一天的日期和时间
	 * 
	 * @return
	 */
	public static String getCurrentMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		return format.format(calendar.getTime());
	}
	
	
	/**
	 * 将时间戳转换为日期
	 * 
	 * @return
	 */
	public static String stampToDate(long time) {
		String dateStr;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(time);
		dateStr = simpleDateFormat.format(date);
		return dateStr;
	}
	
	
	/**
	 * yyyy-MM-dd HH:mm:ss字符串转成日期
	 * 
	 * @return @throws
	 */
	public static Date stringDateConvertToDate(String date) {
		// 转换日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date newDate = null;
		try {
			newDate = dateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return newDate;
	}

	/**
	 * 
	 * @return
	 */
	public static String getFirstDayOfMonth() {
		// 获取当前月第一天：
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		// 设置为1号,当前日期既为本月第一天
		c.set(Calendar.DAY_OF_MONTH, 1);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String first = sdf.format(c.getTime());
		return first;
	}

		/**
	 * 
	 * @return
	 */
	public static String getLastDayOfMonth() {
		// 获取当前月最后一天
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String last = sdf.format(ca.getTime());
		return last;
	}

	/**
	 * 方法描述:  判断日期时间是否在要给范围内
	 * 修改备注: 
	 * @param currentDateTimeStr
	 * @param startDateTimeStr
	 * @param endDateTimeStr
	 * @return
	 */
	public static boolean judgeDateTimeIsInRange(String currentDateTimeStr, String startDateTimeStr, String endDateTimeStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentDateTime = null;
		try {
			currentDateTime = sdf.parse(currentDateTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date startDateTime = null;
		try {
			startDateTime = sdf.parse(startDateTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date endDateTime = null;
		try {
			endDateTime = sdf.parse(endDateTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long currentTime = currentDateTime.getTime();
		long startTime = startDateTime.getTime();
		long endTime = endDateTime.getTime();
		if(currentTime <= endTime && currentTime >= startTime) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 日期格式字符串转换成UNIX时间戳
	 *
	 * @param dateStr
	 * @return
	 */
	public static String Date2UNIXTimeStamp(String dateStr) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 判断时间是否大于1970年。如果大于返回true，如果小于或等于，返回false。
	 *
	 * @param date
	 * @return
	 */
	public static boolean judgeYear(Object date) {
		if (date instanceof String) {
			String year = ((String) date).substring(0, 4);
			return year.compareTo("1970") == 1;
		} else if (date instanceof Date) {
			Calendar c = Calendar.getInstance();
			c.setTime((Date) date);
			return c.get(Calendar.YEAR) > 1970;
		} else if (date instanceof Timestamp) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str = df.format(date);
			String year = str.substring(0, 4);
			return year.compareTo("1970") == 1;
		}
		return false;
	}
	/**
	 * 获取过去第几天的日期时间
	 * 
	 * @param past
	 * @return
	 */
	public static String getPastDateTime(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String result = format.format(today);
		return result;
	}
	
	/**
	 * 获取当前的年月日时分秒毫秒 yyyyMMddHHmmssSSS
	 */
	public static String getDateToMill() {
		Date date = new Date();
		// 转换日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return dateFormat.format(date);
	}
	
	/**
	 * 获取当前的年月日yyyyMMdd
	 * 
	 * @return yyyyMMdd
	 */
	public static String getDateForOss() {
		Date date = new Date();
		// 转换日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		return dateFormat.format(date);
	}

	/**
	 * 获取当天0点
	 * @return
	 */
	public static String getTomorrowStartTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		// 如果当前时间00：00：00
		if ((gc.get(gc.HOUR_OF_DAY) == 0) && (gc.get(gc.MINUTE) == 0) && (gc.get(gc.SECOND) == 0)) {
			date = new Date(date.getTime());
			return dateFormat.format(date);
		} else {
			// 返回昨天的0点
			date = new Date(date.getTime() - gc.get(gc.HOUR_OF_DAY) * 60 * 60 * 1000 - gc.get(gc.MINUTE) * 60 * 1000
					- gc.get(gc.SECOND) * 1000 + 3600 * 24 * 1000);
			return dateFormat.format(date);
		}
	}

	@SneakyThrows
	public static void main(String[] args) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date expireDate = (Date)dateFormat.parseObject(DateTimeUtil.getTomorrowStartTime());
		System.out.println(expireDate);
	}

}
